package trade

import (
	"errors"
	"github.com/gin-gonic/gin"
	uuid "github.com/satori/go.uuid"
	"higame/modules/store"
	"higame/modules/utils"
	"higame/routes/game"
	"higame/routes/user"
	"strings"
)

type postShameForList struct {
	PageNum    int64     `json:"page_num" form:"page_num"`
	Seller     string    `json:"seller" form:"seller"`
	Keyword    string    `json:"keyword" form:"keyword"`
	StartPrice float64   `json:"start_price" form:"start_price"`
	EndPrice   float64   `json:"end_price" form:"end_price"`
	Belongs    string    `json:"belongs" form:"belongs"`
	GoodsType  GoodsType `json:"goods_type" form:"goods_type"`
}

func validatorTrade(ctx *gin.Context) (*Schema, error) {
	t := New()
	if err := ctx.ShouldBind(t); err != nil {
		errText := err.Error()
		var msgList [] string
		if strings.Contains(errText, "'Title' failed on the 'required' tag") {
			msgList = append(msgList, "商品标题不能为空")
		}

		if strings.Contains(errText, "'Price' failed on the 'required' tag") {
			msgList = append(msgList, "价格不能为空")
		}

		if strings.Contains(errText, "'Quantity' failed on the 'required' tag") {
			msgList = append(msgList, "库存不能为空")
		}


		if strings.Contains(errText, "'Seller' failed on the 'required' tag") {
			msgList = append(msgList, "卖家账户不能为空")
		}

		if strings.Contains(errText, "'GoodsType' failed on the 'required' tag") {
			msgList = append(msgList, "商品类型不能为空")
		}

		if strings.Contains(errText, "'Belongs' failed on the 'required' tag") {
			msgList = append(msgList, "游戏种类不能为空")
		}

		if strings.Contains(errText, "'Title' failed on the 'min' tag") {
			msgList = append(msgList, "商品标题不能小于5个字")
		}

		if strings.Contains(errText, "'Title' failed on the 'max' tag") {
			msgList = append(msgList, "商品标题不能大于50个字")
		}

		if len(msgList) > 0 {
			return nil, errors.New(strings.Join(msgList, "，") + "。")
		}
	}

	if _, err := user.FindOneByName(t.Seller); err != nil {
		return nil, errors.New("卖家账户没有注册！")
	}

	if _, err := game.FindByCode(t.Belongs); err != nil {
		return nil, errors.New("你选择的游戏不存在")
	}

	if t.GoodsType != Account && t.GoodsType != Gun && t.GoodsType != Gold {
		return nil, errors.New("交易的商品类型参数错误")
	}

	if t.SafeType != Admin {
		return nil, errors.New("商品的保障类型参数错误")
	}

	detailImage := ctx.PostForm("detail_image")
	if detailImage != "" {
		t.DetailImage = strings.Split(detailImage, ",")
	}
	return &Schema{
		UUID:        t.UUID,
		AddTime:     t.AddTime,
		EndTime:     t.EndTime,
		Title:       t.Title,
		Subtitle:    t.Subtitle,
		Price:       t.Price,
		Quantity:    t.Quantity,
		GoodsType:   t.GoodsType,
		Belongs:     t.Belongs,
		Seller:      t.Seller,
		MainImage:   t.MainImage,
		DetailImage: t.DetailImage,
		SafeType:    t.SafeType,
	}, nil
}

func validatorForUpdate(ctx *gin.Context) (*Schema, error) {
	t := New()
	if err := ctx.ShouldBind(t); err != nil {
		errText := err.Error()
		var msgList [] string
		if strings.Contains(errText, "'Title' failed on the 'required' tag") {
			msgList = append(msgList, "商品标题不能为空")
		}

		if strings.Contains(errText, "'Price' failed on the 'required' tag") {
			msgList = append(msgList, "价格不能为空")
		}

		if strings.Contains(errText, "'Quantity' failed on the 'required' tag") {
			msgList = append(msgList, "库存不能为空")
		}

		if strings.Contains(errText, "'EndTime' failed on the 'required' tag") {
			msgList = append(msgList, "过期时间不能为空")
		}

		if strings.Contains(errText, "'Seller' failed on the 'required' tag") {
			msgList = append(msgList, "卖家账户不能为空")
		}

		if strings.Contains(errText, "'GoodsType' failed on the 'required' tag") {
			msgList = append(msgList, "商品类型不能为空")
		}

		if strings.Contains(errText, "'Belongs' failed on the 'required' tag") {
			msgList = append(msgList, "游戏种类不能为空")
		}

		if strings.Contains(errText, "'Title' failed on the 'min' tag") {
			msgList = append(msgList, "商品标题不能小于5个字")
		}

		if strings.Contains(errText, "'Title' failed on the 'max' tag") {
			msgList = append(msgList, "商品标题不能大于50个字")
		}

		if len(msgList) > 0 {
			return nil, errors.New(strings.Join(msgList, "，") + "。")
		}
	}

	if _, err := user.FindOneByName(t.Seller); err != nil {
		return nil, errors.New("卖家账户没有注册！")
	}

	if _, err := game.FindByCode(t.Belongs); err != nil {
		return nil, errors.New("你选择的游戏不存在")
	}

	if t.GoodsType != Account && t.GoodsType != Gun && t.GoodsType != Gold {
		return nil, errors.New("交易的商品类型参数错误")
	}

	if t.SafeType != Admin {
		return nil, errors.New("商品的保障类型参数错误")
	}

	detailImage := ctx.PostForm("detail_image")
	if detailImage != "" {
		t.DetailImage = strings.Split(detailImage, ",")
	}
	return &Schema{
		UUID:        t.UUID,
		AddTime:     t.AddTime,
		EndTime:     t.EndTime,
		Title:       t.Title,
		Subtitle:    t.Subtitle,
		Price:       t.Price,
		Quantity:    t.Quantity,
		GoodsType:   t.GoodsType,
		Belongs:     t.Belongs,
		Seller:      t.Seller,
		MainImage:   t.MainImage,
		DetailImage: t.DetailImage,
		SafeType:    t.SafeType,
	}, nil
}

func validatorForList(ctx *gin.Context) (*postShameForList, error) {
	p := &postShameForList{}

	if err := ctx.ShouldBind(p); err != nil {
	}
	return p, nil
}

func validatorTradeForMe(ctx *gin.Context) (*Schema, error) {
	t := New()

	if err := ctx.ShouldBind(t); err != nil {
		errText := err.Error()
		var msgList [] string
		if strings.Contains(errText, "'Title' failed on the 'required' tag") {
			msgList = append(msgList, "商品标题不能为空")
		}

		if strings.Contains(errText, "'Price' failed on the 'required' tag") {
			msgList = append(msgList, "价格不能为空")
		}

		if strings.Contains(errText, "'Quantity' failed on the 'required' tag") {
			msgList = append(msgList, "库存不能为空")
		}


		if strings.Contains(errText, "'GoodsType' failed on the 'required' tag") {
			msgList = append(msgList, "商品类型不能为空")
		}

		if strings.Contains(errText, "'Belongs' failed on the 'required' tag") {
			msgList = append(msgList, "游戏类别不能为空")
		}

		if strings.Contains(errText, "'Title' failed on the 'min' tag") {
			msgList = append(msgList, "商品标题不能小于5个字")
		}

		if strings.Contains(errText, "'Title' failed on the 'max' tag") {
			msgList = append(msgList, "商品标题不能大于50个字")
		}

		if len(msgList) > 0 {
			return nil, errors.New(strings.Join(msgList, "，") + "。")
		}
	}

	claims, err := store.GetClaims(ctx)
	if err != nil {
		return nil, err
	}

	if _, err := game.FindByCode(t.Belongs); err != nil {
		return nil, errors.New("你选择的游戏不存在")
	}

	if t.GoodsType != Account && t.GoodsType != Gun && t.GoodsType != Gold {
		return nil, errors.New("交易的商品类型参数错误")
	}

	if t.SafeType != Admin {
		return nil, errors.New("商品的保障类型参数错误")
	}

	detailImage := ctx.PostForm("detail_image")
	if detailImage != "" {
		t.DetailImage = strings.Split(detailImage, ",")
	}
	return &Schema{
		UUID:        t.UUID,
		AddTime:     t.AddTime,
		EndTime:     t.EndTime,
		Title:       t.Title,
		Subtitle:    t.Subtitle,
		Price:       t.Price,
		Quantity:    t.Quantity,
		GoodsType:   t.GoodsType,
		Belongs:     t.Belongs,
		Seller:      claims.Audience,
		MainImage:   t.MainImage,
		DetailImage: t.DetailImage,
		SafeType:    t.SafeType,
	}, nil
}

func validatorForMyUpdate(ctx *gin.Context) (*Schema, error) {
	t := New()

	if err := ctx.ShouldBind(t); err != nil {
		errText := err.Error()
		var msgList [] string
		if strings.Contains(errText, "'Title' failed on the 'required' tag") {
			msgList = append(msgList, "商品标题不能为空")
		}

		if strings.Contains(errText, "'Price' failed on the 'required' tag") {
			msgList = append(msgList, "价格不能为空")
		}

		if strings.Contains(errText, "'Quantity' failed on the 'required' tag") {
			msgList = append(msgList, "库存不能为空")
		}

		if strings.Contains(errText, "'EndTime' failed on the 'required' tag") {
			msgList = append(msgList, "过期时间不能为空")
		}

		if strings.Contains(errText, "'GoodsType' failed on the 'required' tag") {
			msgList = append(msgList, "商品类型不能为空")
		}

		if strings.Contains(errText, "'Belongs' failed on the 'required' tag") {
			msgList = append(msgList, "游戏类别不能为空")
		}

		if strings.Contains(errText, "'Title' failed on the 'min' tag") {
			msgList = append(msgList, "商品标题不能小于5个字")
		}

		if strings.Contains(errText, "'Title' failed on the 'max' tag") {
			msgList = append(msgList, "商品标题不能大于50个字")
		}

		if len(msgList) > 0 {
			return nil, errors.New(strings.Join(msgList, "，") + "。")
		}
	}

	claims, err := store.GetClaims(ctx)
	if err != nil {
		return nil, err
	}

	if _, err := game.FindByCode(t.Belongs); err != nil {
		return nil, errors.New("你选择的游戏不存在")
	}

	if t.GoodsType != Account && t.GoodsType != Gun && t.GoodsType != Gold {
		return nil, errors.New("交易的商品类型参数错误")
	}

	if t.SafeType != Admin {
		return nil, errors.New("商品的保障类型参数错误")
	}

	detailImage := ctx.PostForm("detail_image")
	if detailImage != "" {
		t.DetailImage = strings.Split(detailImage, ",")
	}
	return &Schema{
		UUID:        uuid.NewV1().String(),
		AddTime:     utils.Timestamp(),
		EndTime:     t.EndTime,
		Title:       t.Title,
		Subtitle:    t.Subtitle,
		Price:       t.Price,
		Quantity:    t.Quantity,
		GoodsType:   t.GoodsType,
		Belongs:     t.Belongs,
		Seller:      claims.Audience,
		MainImage:   t.MainImage,
		DetailImage: t.DetailImage,
		SafeType:    t.SafeType,
	}, nil
}
